#pragma once

#include<iostream>
#include<cstring>
#include<string>
#include<assert.h>
using namespace std;

namespace rtx
{
	class string
	{
	public:
		string(const char* s  = "")
		{
			_size =strlen(s);// 因为要算多次strlen 效率低 且放在初始化列表关联到声明顺序 所以不用初始化列表
			_capacity = _size;
			_str = new char[_size + 1];// 开_size+1大小的空间(多开一个放\0)
			strcpy(_str, s);
		}

		//string(const string& s)/////////////////////传统写法
		//	:_str(new char[s._capacity + 1])
		//	, _size(s._size)
		//	, _capacity(s._capacity)
		//{
		//	strcpy(_str, s._str);
		//}
		void swap(string& s)// s和*this换
		{
			::swap(s._str, _str);//注意这里要加域作用符，默认是全局的，不然就是自己调自己了
			::swap(s._size, _size);
			::swap(s._capacity, _capacity);

		}
		string(const string& s)/////////////////////现代写法
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string tmp(s._str);
			swap(tmp);// tmp和*this换
		}

		//string& operator=(const string& s)/////////////////////传统写法
		//{
		//	if (this != &s)
		//	{
		//		char* tmp = new char[s._capacity + 1];// 开辟新的空间
		//		strcpy(tmp, s._str);// 赋值到tmp
		//		delete[] _str;// 释放原有空间

		//		_str = tmp;// tmp赋值到想要的地方，出去tmp就销毁了
		//		_size = s._size;
		//		_capacity = s._capacity;
		//	}
		//	return *this;
		//}		
		string& operator=(const string& s)/////////////////////现代写法
		{
			if (this != &s)
			{
				string tmp(s);
				swap(tmp);// tmp和*this换
			}
			return *this;
		}

		~string() 
		{
			delete[] _str;
			_str = nullptr;
		}

		const char* c_str() const 
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		typedef const char* const_iterator;
		const_iterator begin() const 
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}

		void reserve(size_t new_capacity)
		{
			if (new_capacity > _capacity)
			{
				char* tmp = new char[new_capacity + 1];// 开新空间
				strcpy(tmp, _str);// 搬运
				delete[] _str; // 释放原空间

				_str = tmp;// 没问题，递交给_str
				_capacity = new_capacity;// 更新容量
			}
		}

		void push_back(const char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			_str[_size++] = ch;// 在_size位置放字符后++
			_str[_size] = '\0';// 易漏

			//insert(_size, ch);
		}

		void append(const char* str)
		{
			int len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);// 首字符+_size大小就是\0位置
			_size += len;

			//insert(_size, str);
		}

		string& operator+=(const char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		string& insert(size_t pos, const char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			for (size_t i = _size + 1;i > pos; --i)// 挪动数据,+1是挪动\0
			{
				_str[i] = _str[i - 1];
			}

			_str[pos] = ch;
			++_size;
			return *this;
		}
		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			int len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			for (size_t i = _size + len ;i > pos + len - 1; --i)// 挪动数据，画图注意边界，参考上面inser字符的len == 1
			{
				_str[i] = _str[i - len];// 首先看\0 _size+len-len就是\0的位置
			}

			strncpy(_str + pos, str, len);
			_size += len;
			return *this;
		}

		void resize(size_t new_capacity, const char ch = '\0')
		{
			if (new_capacity > _size)// 插入数据
			{
				reserve(new_capacity);
				//for (size_t i = _size; i < new_capacity; ++i)
				//{
				//	_str[i] = ch;
				//}
				memset(_str + _size, ch, new_capacity - _size);// 上面的for循环即memset的功能
				_str[new_capacity] = '\0';
				_size = new_capacity;
			}
			else// 删除数据
			{
				_str[new_capacity] = '\0';
				_size = new_capacity;
			}
		}

		size_t find(char ch) const
		{
			for (size_t i = 0; i < _size; i++) 
			{
				if (ch == _str[i])// 找到了
				{
					return i;    // 返回下标
				}
			}
			return npos;// 找不到
		}
		size_t find(const char* str, size_t pos = 0) const
		{
			const char* ptr = strstr(_str + pos, str);
			if (ptr == nullptr) 
			{
				return npos;
			}
			else 
			{
				return ptr - _str;  // 减开头
			}
		}

		string& erase(size_t pos, size_t len = npos) 
		{
			assert(pos < _size);
			if (len == npos || pos + len >= _size)// 如果pos后面的都删完了，注意len == npos 不能忽略，因为npos + len 有可能重回到 1
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
			return *this;
		}

		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}
		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}		
		bool operator>=(const string& s) const// 养成this指针写在前面的习惯
		{
			return *this > s || *this == s;
		}
		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}		
		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}		
		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	public:
		const static size_t npos = -1;// const static 语法特殊处理，直接可以当成定义初始化
	};

	ostream& operator<<(ostream& out, string& s)
	{
		for (size_t i = 0;i < s.size();++i)
		{
			out << s[i];
		}
		return out;
	}

	//istream& operator>>(istream& in, string& s)// 流插入普通实现
	//{
	//	char ch;
	//	ch = in.get();
	//	while (ch != ' ' && ch != '\n')
	//	{
	//		s += ch;
	//		ch = in.get();
	//	}
	//	return in;
	//}
	istream& operator>>(istream& in, string& s)// 流插入优化（类似库里面的）
	{
		char ch;
		ch = in.get();

		const size_t N = 32;
		char buff[N];// C++11支持的变长数组
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)// 如果buff的容量满了
			{
				buff[i] = '\0';// 在后面放\0，
				s += buff;// += 到 s 上
				i = 0;// 把 i 重新变成0 用来再次使用buff数组
			}
			ch = in.get();
		}
		buff[i] = '\0';// 处理一下buff剩余的
		s += buff;

		return in;
	}

	void test_string1()
	{
		string s1("hello world");
		string s2(s1);

		string s3("!!!");
		s1 = s3;

		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << s3.c_str() << endl;

		string s4;
		cout << s4.c_str() << endl;
	}
	void test_string2() 
	{
		string s1("hello world");
		string s2;

		for (size_t i = 0; i < s1.size(); i++) 
		{
			cout << s1[i] << " ";
		}
		cout << endl;

		s1[0] = 'x';
		for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		}
		cout << endl;
	}

	void test_string3()
	{
		string s1("hello world");

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << " ";// 读
			it++;
		}
		cout << endl;

		it = s1.begin();
		while (it != s1.end())
		{
			(*it)++;// 写，++的优先级比*高，+=就低，可以 *it += 1;
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto& e : s1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_string4() 
	{
		string s1("hello world");
		cout << s1.c_str() << endl;

		s1.push_back('!');
		cout << s1.c_str() << endl;

		s1.push_back('R');
		cout << s1.c_str() << endl;

		s1.append("abcd");
		cout << s1.c_str() << endl;

		s1 += 'e';
		s1 += "fgh";
		cout << s1.c_str() << endl;

		s1.insert(0, 'x');
		s1.insert(6, 'T');
		cout << s1.c_str() << endl;

		s1.insert(6, "PPPPPPPPPP");
		cout << s1.c_str() << endl;
		s1.insert(0, "PPPPPPPPPP");
		cout << s1.c_str() << endl;

		s1.resize(100,'x');
		cout << s1.c_str() << endl;
	}

	void test_string5()
	{
		string s1("hello world");
		string s2(s1);
		string s3 = s1;
		cout << s2.c_str() << endl << s3.c_str() << endl;

		cout << s1.find('d') << endl;// 打印d的下标:6
		cout << s1.find("world") << endl;// 打印了w的下标:10
		cout << s1.find("wold") << endl;// 打印了npos:4294967295
		cout << s1.find("world", 9) << endl;// 打印了npos:4294967295

		s1.erase(9, 2);// 从下标9开始删除2个字符
		cout << s1.c_str() << endl;

		s1.erase(s1.find('o'), 2);// 找到o，其下标为4，从下标4开始删除2个字符
		cout << s1.c_str() << endl;

		s1.erase(5);// 从下标5开始删完
		cout << s1.c_str() << endl;
	}

	void test_string6()
	{
		string s1;
		string s2;
		cin >> s1 >> s2;
		cout << s1 << endl << s2 << endl;

		cout << (s1 > s2) << endl;
		cout << (s1 == s2) << endl;
		cout << (s1 >= s2) << endl;
		cout << (s1 < s2) << endl;
		cout << (s1 <= s2) << endl;
		cout << (s1 != s2) << endl;
	}
}